package framework.service.transaction.impl;

import framework.dao.exception.DataAccessException;
import framework.dao.jdbc.util.JdbcUtil;
import framework.dao.mybatis.util.MyBatisUtil;
import framework.service.transaction.TransactionManager;
import org.apache.ibatis.session.SqlSession;

import java.sql.Connection;

public class MyBatisTransactionManagerImpl implements TransactionManager {
    @Override
    public void begin() {
        SqlSession session=null;
        try {
            session= MyBatisUtil.getSession();

        } catch (Exception e) {
            throw new DataAccessException("数据访问失败",e);
        }
    }

    @Override
    public void commit() {
        SqlSession session=null;
        try{
            session= MyBatisUtil.getSession();

            session.commit();
        } catch (Exception e) {
            throw new DataAccessException("数据访问失败",e);
        } finally {
            MyBatisUtil.close();
        }
    }

    @Override
    public void rollback() {
        SqlSession session=null;
        try{
            session=MyBatisUtil.getSession();

            session.rollback();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtil.close();
        }
    }
}
